#include "LoggerForTRT.h"

#include <QDebug>
#include <QCoreApplication>

LoggerForTRT::LoggerForTRT(QObject *parent):QObject (parent)
{
    m_isBuildingEngine=false;
    logfile.setFileName("LoggerForTRT.txt");
    logfile.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Truncate);
    logstream.setDevice(&logfile);
}

void LoggerForTRT::setIsBuildingEngine(bool isBuildingEngine)
{
    m_isBuildingEngine = isBuildingEngine;
}

void LoggerForTRT::log(Severity severity, const nvinfer1::AsciiChar *msg) noexcept
{
    QString color="black";
    QString header="INFO: ";
    switch (severity) {
    case Severity::kINTERNAL_ERROR:
        header="INTERNAL_ERROR: ";
        color="red";
        break;
    case Severity::kERROR:
        header="ERROR: ";
        color="red";
        break;
    case Severity::kWARNING:
        header="WARNING: ";
        color="violet";
        break;
    case Severity::kVERBOSE:
        header="VERBOSE: ";
        color="blue";
        break;
    case Severity::kINFO:
    default:
        break;
    }

    QString info = header + QString::fromLocal8Bit(msg);
    if(m_isBuildingEngine){
        if(severity==Severity::kVERBOSE && logfile.isOpen()){
            logstream<<info<<"\n";
            emit toastVerbose(info,color);
        }
    }

    if(severity!=Severity::kVERBOSE){
        emit toastInfo(info,color);
    }

//    if(severity!=Severity::kVERBOSE){
////        qApp->processEvents();
//    }else if(m_isBuildingEngine){
////        emit toastInfo(info,color);
////        qApp->processEvents();
//    }

    qDebug()<<info;
//    if(severity!=Severity::kINFO){

////        qApp->processEvents();
//    }
}


